package cn.itcast;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

/**
 * @program: schedule
 * @description: 执行quartz的定时任务总线程
 * @author: zhanghz001
 * @create: 2021-08-03 20:33
 **/
public class ZhzRunner {
    public static void main(String[] args) throws SchedulerException {
        //创建调度器
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        
        //当前时间5s以后
        Date startTime = DateBuilder.futureDate(10, DateBuilder.IntervalUnit.SECOND);
        //当前时间3h以后
        Date endTime = DateBuilder.futureDate(3, DateBuilder.IntervalUnit.HOUR);
        
        //编写封装任务(JobDetail)
        JobDetail jobDetail = JobBuilder.newJob(ZhzJob.class)
                .withIdentity("zhzJob", "group1")
                //这个是添加参数
                .usingJobData("name", "zhanghz001")
                .build();
        
        //编写Tigger(定时任务的触发机制)
        Trigger trigger = TriggerBuilder.newTrigger()
                .startAt(startTime)
                .endAt(endTime)
                .withSchedule(
                        //简单的定时任务
                        // SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(3)
                        //         //可以执行成次数
                        //         .withRepeatCount(5)
                        
                        //触发时机Trigger,cron表达式
                        CronScheduleBuilder.cronSchedule("3,8,10,20 * * * * ?")
                ).build();
        
        //注册jobDetail和Trigger到scheduled中
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
    }
    
    /**
     * 用来测试初始的quartz的,包括传参,基本的trigger
     *
     * @param args
     * @throws SchedulerException
     */
    public static void main1(String[] args) throws SchedulerException {
        //创建调度器
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        
        //编写封装任务(JobDetail)
        JobDetail jobDetail = JobBuilder.newJob(ZhzJob.class)
                .withIdentity("zhzJob", "group1")
                //这个是添加参数
                .usingJobData("name", "zhanghz001")
                .build();
        
        //编写Tigger(定时任务的触发机制)
        Trigger trigger = TriggerBuilder.newTrigger().withSchedule(
                //简单的定时任务
                SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(3).repeatForever()
        ).build();
        
        //注册jobDetail和Trigger到scheduled中
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
    }
}
